package com.xqx.controller;

import java.lang.annotation.Retention;
import java.sql.ResultSet;
import java.nio.file.attribute.PosixFilePermission;

import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import com.xqx.pojo.User;
import com.xqx.service.UserService;
import com.xqx.utils.ConfigUtils;
import com.xqx.utils.MailUtils;
import com.xqx.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author：xingquanxiang createTime：2019/10/23 10:37
 * description:
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("/sendEmail")
    @ResponseBody
    public boolean sendEmail() {
        return MailUtils.sendMail("xingquanxianga@163.com", "你好！", "测试");
    }

    @GetMapping("/findAll")
    @ResponseBody
    public List<User> findAll() {
        return userService.findAll();
    }

    @GetMapping("/findAllByPage")
    @ResponseBody
    public PageInfo<User> findAllByPage(int pageNum, int pageSize) {
        return userService.findAllByPage(pageNum, pageSize);
    }

    @RequestMapping("findByCondition")
    @ResponseBody
    public User findByCondition(User user) {
        System.out.println("user = " + user);
        return userService.findByCondition(user);
    }

    @GetMapping("/findById")
    @ResponseBody
    public User findById(int id) {
        return userService.findById(id);
    }

    @GetMapping("/findByName")
    @ResponseBody
    public User findByName(String name) {
        return userService.findByName(name);
    }

    @GetMapping("/findLikeName")
    @ResponseBody
    public List<User> findLikeName(String name) {
        return userService.findLikeName(name);
    }


    @RequestMapping("/transfer")
    @ResponseBody
    public boolean transfer(String payer, String receiver, Double money) {
        /*if (payer.isEmpty()||receiver.isEmpty()||money==null){
            return false;
        }
        return userService.doTransfer(payer, receiver, money);*/
        return !payer.isEmpty() && !receiver.isEmpty() && money != null ? this.userService.doTransfer(payer, receiver, money) : false;
    }

    @PostMapping("/save")
    @ResponseBody
    public User save(User user, MultipartFile uploadFile, HttpServletRequest request) throws IOException {
        ServletContext servletContext = request.getServletContext();
        String uploadDirectoryPath = ConfigUtils.getProperties().getProperty("upload.directory");
        String realPath = servletContext.getRealPath(uploadDirectoryPath);
        File uploadDirectory = new File(realPath);
        if (!uploadDirectory.exists()) {
            uploadDirectory.mkdir();
        }
        //获取上传文件名称
        String originalFilename = uploadFile.getOriginalFilename();
        //重命名上传的文件
        originalFilename = UuidUtil.getUuid()+originalFilename.substring(originalFilename.lastIndexOf(".")-1);
        String imagePath = realPath + File.separator + originalFilename;
        //创建文件
        File _file = new File(imagePath);
        //执行上传
        uploadFile.transferTo(_file);
        //判断是否上传成功
        if (_file.exists()){
            //上传成功
            user.setImageUrl(uploadDirectoryPath+ "/" + originalFilename);
        }
        System.out.println(_file.getAbsolutePath());
        return userService.doSave(user);
    }


    @RequestMapping("/delById")
    @ResponseBody
    public int delById(@RequestParam("id") int[] ids) {
        return userService.doDelById(ids);
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/upload.jsp
     * 多文件上传
     */
    @RequestMapping("/upload")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void upload(String username, MultipartFile[] uploadFile, HttpServletRequest request) throws IOException {
        String realPath = request.getServletContext().getRealPath(ConfigUtils.getProperties().getProperty("upload.directory"));
        File uploadDirectory = new File(realPath);
        if (!uploadDirectory.exists()) {
            uploadDirectory.mkdir();
        }
        for (MultipartFile file : uploadFile) {
            //获取上传文件名称
            String originalFilename = file.getOriginalFilename();
            //创建文件
            File _file = new File(realPath + "/" + originalFilename);
            //执行上传
            file.transferTo(_file);
            System.out.println(_file.getAbsolutePath());
        }
    }


}
